package com.toolset.Activity.UserItemActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.toolset.Activity.BaseActivity;
import com.toolset.Activity.RegisterActivity;
import com.toolset.Bean.User;
import com.toolset.R;

import org.litepal.crud.DataSupport;
/*
用户查询个人信息 活动
 */
public class QueryAllInfo extends BaseActivity {

    private EditText userId;
    private EditText userName;
    private EditText userEmail;
    private RadioButton userMan;
    private RadioButton userWoman;
    private Button userUpdateBtn;

    Intent intent;

    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_all_info);

        //初始化事件
        initEvent();

        //获取 父视图发来的数据
        getParentData();

        //设置事件中的数据
        setEventData();

        //设置账号框点击之后弹出不可修改
        userId.setClickable(true);
        userId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QueryAllInfo.this,"账号不可修改！",Toast.LENGTH_SHORT).show();
            }
        });

        //设置修改个人信息
        userUpdateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //检查信息
                if(checkData()){
                    String name = userName.getText().toString();
                    String email = userEmail.getText().toString();
                    String sex;
                    if (userMan.isChecked()){
                        sex = userMan.getText().toString();
                    }else
                        sex = userWoman.getText().toString();

                    //数据库更新数据
                    user.setUserName(name);
                    user.setUserMail(email);
                    user.setUserSex(sex);

                    //数据库修改数据  通过数据库自动生成的Id修改
                    int count  = user.update(user.getId());

                    if(count>0){
                        Toast.makeText(QueryAllInfo.this,"修改成功！",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(QueryAllInfo.this,"修改失败！",Toast.LENGTH_SHORT).show();
                    }

                }

            }
        });

    }

    //初始化事件
    private void initEvent() {
        userId = (EditText) findViewById(R.id.all_Info_UserId);
        userName = (EditText) findViewById(R.id.all_Info_UserName);
        userEmail = (EditText) findViewById(R.id.all_Info_UserEmail);
        userMan = (RadioButton) findViewById(R.id.all_Info_Man);
        userWoman = (RadioButton) findViewById(R.id.all_Info_Woman);
        userUpdateBtn = (Button) findViewById(R.id.all_Info_UserUpdate);
    }


    public void getParentData() {
       intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
    }

    public void setEventData() {
        userId.setText(user.getUserId());
        userName.setText(user.getUserName());
        //设置光标到最后
        userName.setSelection(user.getUserName().length());

        userEmail.setText(user.getUserMail());
        String sex = user.getUserSex();
        if (sex.equals("男")){
            userMan.setChecked(true);
        }else{
            userWoman.setChecked(true);
        }
    }
    //检查信息
    public boolean checkData(){

        //验证昵称
        if (userName.getText().length()==0){
            Toast.makeText(QueryAllInfo.this,"昵称不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }

        // 验证账号
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        String mail = userEmail.getText().toString();
        if (!mail.matches(regex)) {
            Toast.makeText(QueryAllInfo.this,"邮箱格式错误！请重新输入~",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
